/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制
 */
package co.yixiang.modules.adopt.serviceimpl;

import co.yixiang.common.exception.BadRequestException;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.FileUtil;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.modules.adopt.entity.domain.YxAdoptContract;
import co.yixiang.modules.adopt.entity.domain.YxAdoptProduct;
import co.yixiang.modules.adopt.entity.dto.YxAdoptContractDto;
import co.yixiang.modules.adopt.entity.dto.YxAdoptContractQueryCriteria;
import co.yixiang.modules.adopt.mapper.AdoptContractMapper;
import co.yixiang.modules.adopt.service.YxAdoptContractService;
import co.yixiang.modules.adopt.service.YxAdoptProductService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @author hupeng
 * @date 2020-05-12
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxAdoptContractServiceImpl extends BaseServiceImpl<AdoptContractMapper, YxAdoptContract> implements YxAdoptContractService {
    @Autowired
    private IGenerator generator;

    @Autowired
    private YxAdoptProductService adoptProductService;
    @Override
    public Map<String, Object> queryAll(YxAdoptContractQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxAdoptContract> page = new PageInfo<>(queryAll(criteria));
        Map<String, Object> map = new LinkedHashMap<>(2);
        map.put("content", generator.convert(page.getList(), YxAdoptContractDto.class));
        map.put("totalElements", page.getTotal());
        return map;
    }

    @Override
    public List<YxAdoptContract> queryAll(YxAdoptContractQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxAdoptContract.class, criteria));
    }

    @Override
    public YxAdoptContract findById(YxAdoptContract criteria) {
        // 查询到文章信息
        return getById(criteria);
    }

    @Override
    public void download(List<YxAdoptContractDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxAdoptContractDto contract : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("分类id", contract.getCid());
            map.put("合同标题", contract.getTitle());
            map.put("合同图片", contract.getImages());
            map.put("content", contract.getContent());
            map.put("排序", contract.getSort());
            map.put("原文链接", contract.getUrl());
            map.put("添加时间", contract.getAddTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void uploadNews(YxAdoptContractDto yxAdoptContractDTO) throws WxErrorException {

    }

    @Override
    public void delete(Integer id) {
        final Long count = adoptProductService.lambdaQuery().ne(YxAdoptProduct::getContract, id).count();
        if (count > 0) {
            throw new BadRequestException("当前合同下有认养产品不可删除!");
        }
        this.removeById(id);
    }

    @Override
    public void update(YxAdoptContract resources) {
        updateById(resources);
    }

    @Override
    public YxAdoptContract create(YxAdoptContract resources) {
        save(resources);
        return resources;
    }
}
